<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜谱名称" prop="menusName">
        <el-input
          v-model="queryParams.menusName"
          placeholder="请输入菜谱名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tenant:menus:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tenant:menus:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="menusList" @expand-change="exchange" :row-key="getRowKeys" :expand-row-keys="expandRowKeys">
      <el-table-column type="expand"> 
          <template slot-scope="scope">
            <div style="margin-bottom:10px">
              <el-row>  
                <el-col :gutter="10">
                  <el-button type="primary" plain icon="el-icon-plus" size="mini" align="center" @click="handleAddDish(scope.row.menusId)">添加菜肴</el-button>
                </el-col> 
              </el-row>
            </div>
            <el-table :data="dishList" style="width: 100%">
              <el-table-column prop="dishName" label="菜肴名称" width="250" />
              <el-table-column label="单价(元)" width="250">
                <template slot-scope="scope">
                  {{scope.row.price.toFixed(2)}}
                </template>
              </el-table-column>
              <el-table-column prop="unit" label="规格" width="250" :formatter="unitFormat" />
            </el-table>
          </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="menusId" />
      <el-table-column label="菜谱名称" align="center" prop="menusName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建者" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tenant:menus:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tenant:menus:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改菜谱对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="菜谱名称" prop="menusName">
          <el-input v-model="form.menusName" placeholder="请输入菜谱名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
     <!-- 添加或修改菜谱的菜肴对话框 -->
    <el-dialog :title="title" :visible.sync="dishOpen" width="700px"  append-to-body>
      <template>
        <el-transfer
          filterable
          filter-placeholder="请输入菜肴拼音"
          v-model="dishValue"
          :data="dishData">
        </el-transfer>
      </template>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDish">确 定</el-button>
        <el-button @click="cancelDish">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMenus, getMenus, delMenus, addMenus, updateMenus } from "@/api/tenant/menus";
import { listDish } from "@/api/tenant/dish";
import { listDishIdsByMenusId, listDishByMenusId, addMenusDish } from "@/api/tenant/menusDish";

export default {
  name: "Menus",
  components: {
  },
  data() {
    
    return {
      dishData: [],
      dishValue: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 菜谱表格数据
      menusList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示菜谱菜肴弹出层
      dishOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menusName: null,
      },
      // 表单参数
      form: {},
      // 菜谱菜肴表单参数
      dishForm: {},
      // 表单校验
      rules: {
        menusName: [
          { required: true, message: "菜谱名称不能为空", trigger: "blur" }
        ],
      },
      dishList: [],
      expandRowKeys: []
    };
  },
  created() {
    this.getList();
    this.getDicts("tenant_dish_unit").then(response => {
      this.unitOptions = response.data;
    });
    
  },
  methods: {

    /** 查询菜谱列表 */
    getList() {
      this.loading = true;
      listMenus(this.queryParams).then(response => {
        this.menusList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        menusId: null,
        menusName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.menusId)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加菜谱";
    },
    /**添加菜肴按钮操作 */
    async handleAddDish(meunusId) {
      this.title = "添加菜肴至菜谱";
      this.dishForm.menusId = meunusId; 
       const {rows:allDishList} = await listDish();
      const {data:dishIds} = await listDishIdsByMenusId(meunusId);
      const data=[]
      allDishList.forEach(obj =>{
        data.push({
          label: obj.dishName,
          key: obj.dishId
        })
      });
    this.dishData = data;
    this.dishValue = dishIds
    this.dishOpen = true;
    },
    submitDish() {
      this.dishForm.dishIds = this.dishValue
      addMenusDish(this.dishForm).then(response => {
        this.msgSuccess("修改成功");
        this.dishOpen = false;
        listDishByMenusId(this.dishForm.menusId).then(response => {
         this.dishList = response.data
        });
        
      })
    },
    cancelDish() {
       this.dishOpen = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const menusId = row.menusId || this.ids
      getMenus(menusId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改菜谱";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.menusId != null) {
            updateMenus(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMenus(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const menusIds = row.menusId || this.ids;
      this.$confirm('是否确认删除菜谱编号为"' + menusIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMenus(menusIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tenant/menus/export', {
        ...this.queryParams
      }, `tenant_menus.xlsx`)
    },
    /** 展开菜谱 */
    async exchange(row, expandedRows) {
      this.dishList =[]
      if(expandedRows.length ===0){
         this.expandRowKeys = []
        return;
      } 
      this.expandRowKeys = [row.menusId]
      const data = await listDishByMenusId(row.menusId)
      this.dishList = data.data
    },
    /** 赋值 行key */
    getRowKeys(row) {
       return row.menusId
     },
    unitFormat(row, column) {
      return this.selectDictLabel(this.unitOptions, row.unit);
    }
  }
};
</script>
<style>
.el-dialog__body{
    overflow-y: auto;
  }
.el-transfer-panel__body {
  height: 400px  !important;
}
.el-transfer-panel__list.is-filterable{
  height: 380px  !important;
}
</style>